package com.WaVel.controller.admin;

import com.Emplyment.entity.UserRegister;
import com.Emplyment.properties.JwtProperties;
import com.Emplyment.result.Result;
import com.Emplyment.utils.jwtUtils;
import com.Emplyment.vo.EmplyeeLoginVo;
import com.WaVel.service.EmplyeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Slf4j
public class EmplyeeController {

    @Autowired
    private EmplyeeService emplyeeService;
    @Autowired
    private JwtProperties jwtProperties;

    @PostMapping("/register")
    public Result EmplyeeRegister(@RequestParam String username,@RequestParam String password){
        emplyeeService.UserRegister(username,password);
        log.info("注册的用户名与密码为:{}",username+"密码为:{}"+password);
        return Result.success();
    }

    @PostMapping("/login")
    //员工登录
    public  Result<EmplyeeLoginVo> EmplyeeLogin(@RequestParam String username,@RequestParam String password){

        UserRegister userRegister = emplyeeService.UserLogin(username, password);
        log.info("登录的账号为:{}",username+"登录的密码为:+{}",password);

        //登录成功，生成jwt令牌
        Map<String,Object> claims = new HashMap<>();
        claims.put("emp_id",userRegister.getId());
        String token = jwtUtils.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims
        );

        EmplyeeLoginVo emplyeeLoginVo = new EmplyeeLoginVo();
        BeanUtils.copyProperties(userRegister,emplyeeLoginVo);
        emplyeeLoginVo.setToken(token);

        return Result.success(emplyeeLoginVo);
    }

}
